
func! Myomni(st,base)
	if a:st
		"找到第一个非字母的位置"
		let line = getline('.')
		let start = col('.')-1
		while start>0 && line[start-1]=~'\(\a\|\d\|\.\)'
			let start-=1
		endwhile
		return start
	else
		let res = []
		let path = '' 		"file path
		let pre = '' 		"添加时的前部分
		let prepath = ''	"不含o3dict
		let first = 1 		"flag for the first word
		let lw = 'o3dict' 	"last word
		for each in split(a:base,'\.')
			if first
				let path = lw
				let first = 0
			else
				let path .= '/' . lw
				let pre.= lw . '.'
				let prepath .= '/' . lw
			endif
			let lw = each
		endfor

		"尝试打开全部输入组成的文件
		let found = 1
		let fn = path . '/' . lw . '/cur.dict'
		if filereadable(fn)		"若存在则pre.=lw.'.', lw=''
			let pre .= lw . '.'
			let lw = ''
		else				"否则除去lw
			let fn = path . '/cur.dict'
			if !filereadable(fn) 	"若仍不存在则标记
				let found = 0
			endif
		endif

		let wf = 0
		if found "若文件存在
			let cnt = 1
			let info = ""
			let word = ""
			"打开字典文件搜索"
			for each in readfile(fn)
				if cnt % 2 
					let info=each
				else
					let word=pre.each
					if each =~ '^' . lw . '.*'
						call add(res,{"word":word, "info":info})
						let wf += 1
					endif
				endif
				let cnt += 1
			endfor
		endif
		"若没有匹配或无prepath,则在所有目录下查找名为prepath的目录,有则匹配dict
		if !wf || !strlen(prepath)
			if !strlen(prepath) "若无prepath，则设置prepath为lw
				let prepath = lw
				let pre = lw.'.'
				let lw = ''
			end
			let fpath = system("ls -R o3dict | grep .*" . prepath . "/" . lw . ":")
			let fpath = strpart(fpath, 0, stridx(fpath,':'))
			let fpath .= "/cur.dict"
			if filereadable(fpath)
				let pre .= lw.'.'
				let lw = ''
			else
				let fpath = system("ls -R o3dict | grep .*" . prepath . ":")
				let fpath = strpart(fpath, 0, stridx(fpath, ':'))
				let fpath .= "/cur.dict"
			end
			
			let cnt = 1
			let info = ""
			let word = ""
			if filereadable(fpath)
				for each in readfile(fpath)
					if cnt % 2
						let info=each
					else
						let word=pre.each
						if each =~ '^' . lw . '.*'
							call add(res, {"word":word,"info":info})
						endif
					endif
					let cnt += 1
				endfor
			endif
		endif

		return res
	endif
endfunc

set omnifunc=Myomni
